package com.android.nodejs.livejs.adapters;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.android.nodejs.livejs.R;
import com.android.nodejs.livejs.models.MyMessage;

public class MessageAdapter extends ArrayAdapter<MyMessage> {
	public MessageAdapter(Context context, List<MyMessage> msgs) {
		super(context, R.layout.fragment_message_item, msgs);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		final ViewHolder holder;
		if (row == null) {
			row = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.fragment_message_item, null);
			holder = new ViewHolder();
			holder.tvName = (TextView) row.findViewById(R.id.tvName);
			holder.tvTime = (TextView) row.findViewById(R.id.tvTime);
			holder.tvMessage = (TextView) row.findViewById(R.id.tvMessage);
			row.setTag(holder);
		} else {
			holder = (ViewHolder) row.getTag();
		}
		MyMessage msg = getItem(position);
		holder.tvName.setText(msg.getNameSender());
		List<String> t = msg.getTime();
		holder.tvTime.setText(t.get(t.size()-1));
		List<String> m = msg.getMessage();
		holder.tvMessage.setText(m.get(m.size()-1));
		return row;
	}

	private class ViewHolder {
		TextView tvName, tvTime, tvMessage;
	}

}
